﻿using BCL.BaseLib;
using BCL.BaseLib.Basics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BCL.WPFComponent
{
    /// <summary>
    /// MainControl.xaml 的交互逻辑
    /// </summary>
    public partial class MainControl : UserControl, ICustom
    {
        private List<TabHeadItem> headItems;

        private TabHeadItem currentheadItem;
        private TabHeadItem CurrentheadItem
        {
            set
            {
                if (currentheadItem != null && currentheadItem != value)
                {
                    currentheadItem.IsSelect = false;
                    ((Control)(currentheadItem.Tag)).Visibility = Visibility.Hidden;
                }

                currentheadItem = value;
                if (currentheadItem.Tag == null)
                    return;
                ((Control)(currentheadItem.Tag)).Visibility = Visibility.Visible;

            }
        }


        public MainControl()
        {
            headItems = new List<TabHeadItem>();
            InitializeComponent();
        }


        public bool FunctionInvoke(FunctionEntity fe)
        {
            TabHeadItem hi = headItems?.FirstOrDefault(o => o.Function.AppId == fe.AppId);
            if (hi != null)
            {
                hi.IsSelect = true;
                CurrentheadItem = hi;
                return true;
            }

            hi = new TabHeadItem(fe);
            hi.Margin = new Thickness(0,0,10,0);
            hi.SelectChanged += Hi_SelectChanged;
            hi.CloseIng += Hi_CloseIng;
            headItems.Add(hi);
            TapPanel.Children.Add(hi);
            Type type = fe.Control?.GetType();
            if (type == null)
                return false;
            if (type.IsSubclassOf(typeof(Control)))
            {
                Control element = (Control)fe.Control;
                element.HorizontalAlignment = HorizontalAlignment.Stretch;
                element.VerticalAlignment = VerticalAlignment.Stretch;
                element.Margin = new Thickness(10);
                hi.Tag = element;
                MainGrid.Children.Add(element);
                //MainGrid.Child = element;
            }
            hi.IsSelect = true;
            CurrentheadItem = hi;
            return true;
        }

        public bool CloseFunction(FunctionEntity fe)
        {
            headItems.FirstOrDefault(o => fe == o.Function)?.Close();
            return true;
        }


        private void Hi_CloseIng(TabHeadItem obj)
        {
            int index = headItems.IndexOf(obj);
            headItems.RemoveAt(index);
            TapPanel.Children.Remove(obj);
            MainGrid.Children.Remove((Control)obj.Tag);
            if (obj.IsSelect && headItems.Count > 0)
            {
                TabHeadItem next = headItems[headItems.Count == index ? 0 : index];//.IsSelect = true;
                next.IsSelect = true;
                CurrentheadItem = next;
            }
               

        }

        private void Hi_SelectChanged(TabHeadItem obj)
        {
            CurrentheadItem = obj;
        }

        private void xtraTabControl1_CloseButtonClick(object sender, EventArgs e)
        {
            //ClosePageButtonEventArgs cpb = (ClosePageButtonEventArgs)e;
            //XtraTabPage xp = (XtraTabPage)cpb.Page;
            //this.xtraTabControl1.TabPages.Remove(xp);
            //xp.Dispose();
            //tabControlChanged?.Invoke(xp.Tag as FunctionEntity, 1);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
